package cms.utils.threadLocal;

import cms.bean.user.AccessUser;
import cms.utils.SpringContextHolder;
import cms.web.util.WsWebSessionUtil;
import org.springframework.data.redis.core.RedisTemplate;

import javax.servlet.http.HttpSession;
import java.util.concurrent.TimeUnit;

/**
 * 访问用户参数传递
 *
 */
public class AccessUserThreadLocal {

	/**
	 * 设置参数
	 * @param accessUser 用户
	 */
	public static void set(AccessUser accessUser){
		SpringContextHolder.getBean(RedisTemplate.class).opsForValue().set("user:" + accessUser.getUserId(), accessUser, 1, TimeUnit.HOURS);
	}

	public static AccessUser get(HttpSession session) {
		return get(session,true);
	}
	public static AccessUser get(HttpSession session,boolean ckLogin) {
		Long userId = WsWebSessionUtil.getUid(session,ckLogin);
		if(userId==null){
			return null;
		}
		return (AccessUser)SpringContextHolder.getBean(RedisTemplate.class).opsForValue().get("user:" + userId);
	}
	/**
	 * cms.web.filter.TempletesInterceptor中删除
	 */
	public static void removeThreadLocal() {
		//一定要清理，否则就是线程不安全的，因为服务器都是有线程池的。 
//		set(null);
//		holder.remove();
	}
}
